package com.app.worldcupapp.login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.app.worldcupapp.R;
import com.app.worldcupapp.bean.LoginBean;
import com.app.worldcupapp.bean.RegistBean;
import com.app.worldcupapp.countdown.MainActivity;
import com.app.worldcupapp.list.TeamsListActivity;
import com.app.worldcupapp.regiter.RegistActivity;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class LoginActivity extends AppCompatActivity{
    EditText userNameId,userPasswordId;
    Button login;
    TextView forgetPasswordId,userRegisterId;
    String userName,userPassword;
    String TAG = "LoginActivity";
    ImageView loginBack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();

        userRegisterId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this, RegistActivity.class);
                startActivity(intent);
            }
        });

        loginBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                if (!(userName.equals("")||userPassword.equals(""))){

                    login();

                }else {
                    Toast.makeText(LoginActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                }
            }


        });


    }


    private void initData() {
        userName = userNameId.getText().toString();
        userPassword = userPasswordId.getText().toString();
    }

    private void initView() {
        userNameId = findViewById(R.id.userName);
        userPasswordId = findViewById(R.id.userPassword);
        login = findViewById(R.id.login);
        forgetPasswordId = findViewById(R.id.forgetPassword);
        userRegisterId = findViewById(R.id.userRegister);
        loginBack = findViewById(R.id.loginBack);

    }
    private void login(){
        //创建Client
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .add("name",userName)
                .add("password",userPassword)
                .build();
        //创建Request对象
        final Request request = new Request.Builder()
                .url("http://192.168.27.206:8080/app16/user/login")
                .post(body)
                .build();

        final Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG,"网络访问失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Looper.prepare();
                String data = response.body().string();
                Gson gson = new Gson();
                LoginBean loginBean = gson.fromJson(data, LoginBean.class);
                if (loginBean.isSuccess()){
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this, TeamsListActivity.class);
                    startActivity(intent);
                }else {
                    Toast.makeText(LoginActivity.this, "用户名或密码不正确", Toast.LENGTH_SHORT).show();
                }
                Looper.loop();
            }
        });
    }


}